package cn.picclife.mwx.manager.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.picclife.mwx.manager.sys.model.bo.UserSessionBo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.picclife.mwx.manager.sys.mapper.BranchMapper;
import cn.picclife.mwx.manager.sys.model.bo.BranchBo;
import cn.picclife.mwx.manager.sys.model.bo.PageCount;
import cn.picclife.mwx.manager.sys.model.po.Branch;
import cn.picclife.mwx.manager.sys.service.BranchService;

import javax.servlet.http.HttpServletRequest;

@Service
public class BranchServiceImpl implements BranchService {
	private Log log = LogFactory.getLog(BranchServiceImpl.class);
	@Autowired
	private BranchMapper branchMapper;
	
	@Override
	public PageCount queryBranchPage(BranchBo branch) {
		// TODO Auto-generated method stub
		int allCount = branchMapper.queryBranchCount(branch);
		int allPages = allCount / branch.getRowNum();

		if (allCount % branch.getRowNum() > 0) {
			allPages += 1;
		}

		branch.setAllPages(allPages);
		branch.setAllCount(allCount);
		int minTotal = (branch.getNowPage() - 1) * branch.getRowNum();
		int maxTotal = branch.getNowPage() * branch.getRowNum();
		branch.setMinTotal(minTotal);
		branch.setMaxTotal(maxTotal);
		branch.setMaxminTotal(maxTotal-minTotal);
		List<Branch> branchList = branchMapper.queryBranchPage(branch);
		branch.setDatas(branchList);
		return branch;
	}

	@Override
	public int insertBranch(Branch branch) {
		// TODO Auto-generated method stub
		Branch bra = branchMapper.queryBranch(branch.getBranchId());
		if(bra!= null)return 2;
		String branchid = branch.getBranchId();
		String branchAllpath = "1000000";
		int len = branchid.length();
		switch (len) {
			case 4:
				branchAllpath = branchAllpath + "-" + branchid;
				break;
			case 6:
				branchAllpath = branchAllpath + "-" + branchid.substring(0,4) + "-" + branchid;
				break;
			case 8:
				branchAllpath = branchAllpath + "-" + branchid.substring(0,4) + "-" + branchid.substring(0,6) + "-" + branchid;
				break;
	
			default:
				break;
		}
		Integer seqId = branchMapper.querymaxId() + 1;
		
		branch.setBranchId(branchid);
		branch.setBranchParentid("10");
		branch.setBranchAllpath(branchAllpath);
		branch.setSeqId(seqId);
		branch.setStatus("1");
		branch.setBranchLevel("2");
		
		
		return branchMapper.insertBranch(branch);
	}

	@Override
	public Branch queryBranch(String branchId) {
		// TODO Auto-generated method stub
		return branchMapper.queryBranch(branchId);
	}

	@Override
	public int updateBranch(Branch branch) {
		int flag = 0;
		try {
			branchMapper.updateBranch(branch);
			flag = 1;
		} catch (Exception e) {
			flag = 0;
			log.error(e);
		}
		return flag;
	}
	
	@Override
	public int deleteBranch(Branch branch) {
		int flag = 0;
		if("".equals(branch.getStatus())|| branch.getStatus() == null){
			branch.setStatus("0");
		}
		try {
			branchMapper.updateBranch(branch);
			flag = 1;
		} catch (Exception e) {
			flag = 0;
			log.error(e);
		}
		return flag;
	}

	@Override
	public List<Branch> queryBranch1(Branch branch, HttpServletRequest request) {
		List<Branch> list=branchMapper.queryBranch1(branch);
		UserSessionBo userSessionBo = (UserSessionBo) request.getSession().getAttribute("user");
		String deptNo = userSessionBo.getDeptNo();
		String level = userSessionBo.getDeptLevel();
		List<Branch> list1 =new ArrayList<>();
		if ("1".equals(level)) {
			list1=list;
		}else{
			for (Branch branch1 : list) {
				if(branch1.getBranchId()!=null&&branch1.getBranchId().equals(deptNo)){
					list1.add(branch1);
				}
			}
		}
		return list1;
	}

}
